package cn.plushine.controller;

import cn.plushine.pojo.*;
import cn.plushine.service.BasePOrderService;
import cn.plushine.service.BaseTransferService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 调拨订单
 */
@Api(tags = "调拨订单")
@Controller
@RestController
@RequestMapping("/admin/product/transfer")
public class BaseTransferController {

    @Autowired
    BaseTransferService baseTransferService;

    // 获取调拨订单
    @ApiOperation(value = "获取调拨订单")
    @GetMapping("/get")
    public HashMap<String, Object> get() {
        try {
            // 获取调拨订单
            List<Transfer> transfersDB = baseTransferService.get();

            // 货物种类（size），货物总数（sum），购货总价（total），状态判断（flag）
            HashSet<Integer> hashSet = new HashSet<Integer>();
            int sum, flag;
            for (Transfer transfer : transfersDB) {
                hashSet.clear();
                sum = 0;
                flag = 0;
                for (TransferItem transferItem : transfer.getTransfers()) {
                    hashSet.add(transferItem.getGoodsId());
                    sum += transferItem.getNum();
                    flag += transferItem.getFlag();
                }
                transfer.setSize(hashSet.size());
                transfer.setSum(sum);
                // 订单状态判断
                transfer.setFlag(flag == 0 ? 0 : (flag == sum ? 2 : 1));
            }
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取调拨订单成功！", transfersDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取调拨订单失败！", e.getMessage());
        }
    }

    // 获取仓库信息（库-区-位）
    @ApiOperation(value = "获取仓库信息（库-区-位）")
    @GetMapping("/warehouse")
    public HashMap<String, Object> warehouse() {
        try {
            // 获取仓库信息（库-区-位）
            List<Cascader> warehouseDB = baseTransferService.warehouse();
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取仓库信息（库-区-位）成功！", warehouseDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取仓库信息（库-区-位）失败！", e.getMessage());
        }
    }

    // 获取货物列表
    @ApiOperation(value = "获取货物列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "库位id", required = true, paramType = "path", dataType = "int", example = "1")
    })
    @GetMapping("/getGoods/{id}")
    public HashMap<String, Object> getGoods(@PathVariable int id) {
        try {
            // 获取货物列表
            List<Goods> goodsDB = baseTransferService.getGoods(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取货物列表成功！", goodsDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取货物列表失败！", e.getMessage());
        }
    }

    // 生成调拨订单id
    @ApiOperation(value = "生成调拨订单id")
    @GetMapping("/getId")
    public HashMap<String, Object> getId() {
        try {
            // 生成调拨订单id
            String id = "DB" + System.currentTimeMillis();
            // 成功响应
            return ResMapUtils.encapsulation(200, "生成调拨订单id成功！", id);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "生成调拨订单id失败！", e.getMessage());
        }
    }


    // 查询货物单位（库存、品牌）
    @ApiOperation(value = "查询货物单位（库存、品牌）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "货物id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "o3", value = "库位id", required = true, paramType = "path", dataType = "int", example = "1")
    })
    @GetMapping("/getUnit/{id}/{o3}")
    public HashMap<String, Object> getUnit(@PathVariable int id, @PathVariable int o3) {
        try {
            // 查询货物单位（库存、品牌）
            TransferItem unitDB = baseTransferService.getUnit(id, o3);
            // 成功响应
            return ResMapUtils.encapsulation(200, "查询货物单位（库存、品牌）成功！", unitDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "查询货物单位（库存、品牌）失败！", e.getMessage());
        }
    }

    // 添加调拨订单
    @ApiOperation(value = "添加调拨订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "调拨订单编号", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "date", value = "创建日期", required = true, paramType = "path", dataType = "Date"),
            @ApiImplicitParam(name = "o1", value = "调出仓库id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "o2", value = "调出分区id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "o3", value = "调出库位id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "i1", value = "调入仓库id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "i2", value = "调入分区id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "i3", value = "调入库位id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "transferItem", value = "订单内容", required = true, paramType = "body", dataType = "List<TransferItem>")
    })
    @PostMapping("/add/{id}/{date}/{o1}/{o2}/{o3}/{i1}/{i2}/{i3}")
    public HashMap<String, Object> add(@PathVariable String id, @PathVariable Date date, @PathVariable int o1, @PathVariable int o2, @PathVariable int o3, @PathVariable int i1, @PathVariable int i2, @PathVariable int i3, @RequestBody List<TransferItem> transferItems) {
        try {
            // 添加调拨订单
            baseTransferService.add(id, date, o1, o2, o3, i1, i2, i3);
            // 给货物绑定调拨订单
            for (TransferItem transferItem : transferItems) {
                baseTransferService.addTransferItem(id, transferItem.getGoodsId(), o3, transferItem.getNum());
            }
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加调拨订单成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "添加调拨订单失败！", e.getMessage());
        }
    }

    // 获取调拨货物信息
    @ApiOperation(value = "获取调拨货物信息")
    @GetMapping("/getTransfer/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "调拨订单id", required = true, paramType = "path", dataType = "String")
    })
    public HashMap<String, Object> getTransfer(@PathVariable String id) {
        try {
            // 获取调拨货物信息
            List<TransferItem> transferDB = baseTransferService.getTransfer(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取调拨货物信息成功！", transferDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取调拨货物信息失败！", e.getMessage());
        }
    }

    // 批量调拨
    @ApiOperation(value = "批量调拨")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "调拨订单id", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "multiplySelect", value = "待调拨的货物信息", paramType = "body", dataType = "ArrayList<TransferItem>")
    })
    @PutMapping("/modify/{id}")
    public HashMap<String, Object> modify(@PathVariable String id, @RequestBody ArrayList<TransferItem> multiplySelect) {
        try {
            // 获取调拨订单对应的调入位置
            Transfer positionDB = baseTransferService.getPosition(id);
            // 批量调拨
            baseTransferService.modify(multiplySelect, positionDB.getInWarehouseId(), positionDB.getInAreaId(), positionDB.getInPositionId());
            // 成功响应
            return ResMapUtils.encapsulation(200, "批量调拨成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "批量调拨失败！", e.getMessage());
        }
    }

    // 取消调拨订单
    @ApiOperation(value = "取消调拨订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "调拨订单id", required = true, paramType = "path", dataType = "String")
    })
    @DeleteMapping("/cancel/{id}")
    public HashMap<String, Object> cancel(@PathVariable String id) {
        try {
            // 删除调拨订单
            baseTransferService.delete(id);
            // 恢复货物信息
            baseTransferService.cancel(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "取消调拨订单成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "取消调拨订单失败！", e.getMessage());
        }
    }
}
